<%--
  Created by IntelliJ IDEA.
  User: 86183
  Date: 2022/5/5
  Time: 20:02
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>评价</title>
    <link rel="shortcut icon" href="#"/>
    <style>
        body{
            color: white;
            background-color: black;
        }
    </style>
    <meta charset="utf-8">
</head>
<body>
    <div id="messages">
    </div>
    <div id="foot">
        <button id="goBack" onclick="goBack()">返回</button>
        <button id="comment" onclick="Comment()">评论</button><div id="commentDiv"></div>
        <button onclick="beforePage()">上一页</button>
        <button onclick="nextPage()">下一页</button>
    </div>
</body>
<script type="application/javascript">
    let Message;
    let Page;
    let PageNow=1;
    function thePage(objects){
        let page=objects.length/4;
        if(objects.length%4 == 0){return page;}
        return page+1;
    }
    function nextPage(){
        console.log(PageNow)
        console.log(Page)
        if(Page-(PageNow+1)>=0){innerMessages(Message,++PageNow)}else{alert("这是最后一页了!")};
    }
    function beforePage(){
        if(PageNow>1){innerMessages(Message,--PageNow)}else{alert("这是第一页了!")};
    }
    function getCookie(name) {
        if (document.cookie.length > 0) {
            let start = document.cookie.indexOf(name + "=");
            if (start != -1) {
                start = start + name.length + 1;
                let end = document.cookie.indexOf(";", start);
                if (end == -1) end = document.cookie.length;
                return decodeURI(document.cookie.substring(start, end));
            }
        }
        return null;
    }
    let role=getCookie("role");
    let params = new URLSearchParams(location.search);
    let id = params.get('shopId');
    function goBack(){
        if(role=='商家'){
            window.location="Seller.jsp";
        }else{
            window.location="index.jsp";
        }
    }
    function getMessage(){
        let json={
            "shopId":id,
            "mode":"getMessage"
        }
        let xhr=new XMLHttpRequest();
        xhr.open("post","http://localhost:80/intoShop",true);
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        xhr.send("json="+JSON.stringify(json));
        xhr.responseType='json';
        xhr.onreadystatechange=function (){
            if(xhr.status==200 && xhr.readyState==4){
                let data=xhr.response;
                console.log(data);
                if(data.code==404){
                    document.getElementById("messages").innerHTML+=data.state;
                }else{
                    Message=JSON.parse(data.data);
                    console.log(Message)
                    Page=thePage(Message);
                    innerMessages(Message,1);
                }
            }
        }
    }
    function innerMessages(Message,page){
        let count=4;
        if(Page-page<1){
            count=Message.length-(page-1)*4;
        }
        console.log("count:"+count);
        console.log("page:"+page);
        console.log("Page:"+Page)
        document.getElementById("messages").innerHTML='';
        for(let i=4*(page-1);i<count+4*(page-1);i++) {
            document.getElementById("messages").innerHTML +=
                '<div>'+
                '<div style="display:inline-block;">' +
                '<img border="0" src="http://localhost/picture/upload/'+Message[i]["senderPicture"]+
                '" alt="picture" width="20" height="20"></div>'+
                '<div style="display:inline-block; margin-left: 10px">'+
                    Message[i]["senderName"]+
                '</div>';
            if(Message[i]["parentId"]!=0){
                for(let j=0;j<Message.length;j++){
                    if(Message[j]["id"]==Message[i]["parentId"]){
                        document.getElementById("messages").innerHTML+=
                            '<style>text {  font-size: 10px;margin-top: 10px;}</style><text style="font-size: 10px">回复 @'+
                            Message[j]["senderName"]+'</text>' +
                            '<text style="font-size: 10px;margin-top: 10px;">   ['+Message[j]["content"]+']</text>';
                    }
                }
            }
            console.log(Message[i]);
            document.getElementById("messages").innerHTML+=
            `</div><div id="content" style="margin-top: 10px;"><text>`+Message[i]["content"]+`</text></div>
            <div style="margin-top: 10px;">留言时间:`+Message[i]["leftTime"]+`   likes:`+Message[i]["likes"]+`
            <button id="`+Message[i]["id"]+`" style="margin-left: 5px"
            onclick="like('`+Message[i].content+`','`+Message[i]["senderId"]+`','`+Message[i]["id"]+`')">点赞</button>
            <button id="-`+Message[i]["id"]+`'"
            onclick="comment('`+Message[i].content+`','`+Message[i]["senderId"]+`','`+Message[i]["id"]+`')">评论</button>
            <div id="div`+Message[i]["id"]+`"></div></div>`;
            if(role=='商家'){
                document.getElementById("messages").innerHTML+=`<button id="delete`+i+
                    `" onclick="deleteMessage('`+Message[i]["id"]+`')" >删除留言</button><hr>`;
            }else{
                document.getElementById("messages").innerHTML+=`<hr>`;
            }
        }
    }
    function deleteMessage(i){
        let message={
            "messageId":i,
            "mode":"deleteMessage"
        }
        let xhr=new XMLHttpRequest();
        xhr.open("post","http://localhost:80/intoShop",true);
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        xhr.responseType='json';
        xhr.send("json="+JSON.stringify(message));
        xhr.onreadystatechange=function(){
            if(xhr.status==200 && xhr.readyState==4){
                let data = xhr.response;
                console.log(data);
                if(data["code"]==200){
                    console.log("check200:"+data.state)
                    alert(data.state);
                    console.log(data.data);
                    window.location="Message.jsp?shopId="+id;
                }else{
                    alert(data.state);
                    console.log("check404:"+data.state)
                }
            }
        }
    }
    function Comment(){
        document.getElementById("commentDiv").innerHTML+=
            `<textarea id="Content" placeholder="请输入您的评论" maxlength="255"></textarea>
            <button onclick="CheckMessage()">完成</button>`;
    }
    function CheckMessage(){
        let content=document.getElementById("Content").value;
        console.log(content);
        if(content.trim()==''){alert("评论不能为空!");return;}
        let xhr=new XMLHttpRequest();
        xhr.open("post","http://localhost:80/sensitiveCheck",true);
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        xhr.responseType='json';
        xhr.send("content="+JSON.stringify(content));
        xhr.onreadystatechange=function(){
            if(xhr.status==200 && xhr.readyState==4){
                let data = xhr.response;
                console.log(data);
                if(data["code"]==200){
                    console.log("check200:"+data.state)
                    sendContent(content);
                }else{
                    alert(data.state);
                    console.log("check404:"+data.state)
                }
            }
        }
    }
    function sendContent(content){
        let message={
            "content":content,
            "senderId":getCookie("id"),
            "shopId":id,
            "mode":"content"
        }
        let xhr=new XMLHttpRequest();
        xhr.open("post","http://localhost:80/notice",true);
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        console.log(JSON.stringify(message))
        xhr.send("json="+JSON.stringify(message));
        xhr.onreadystatechange=function (){
            if(xhr.status==200 && xhr.readyState==4){
                let data=xhr.response;
                let json=JSON.parse(data);
                console.log(json);
                alert(json.state);
                window.location="Message.jsp?shopId="+id;
            }
        }

    }
    function comment(content,senderId,divId){
        document.getElementById('div'+divId).innerHTML+=
            `<textarea id="reContent`+divId+`" placeholder="请输入您的评论" maxlength="255"></textarea><button onclick="checkMessage('`+
            content+`','`+senderId+`','`+divId+`')">完成</button>`;
    }
    function checkMessage(content,senderId,parentId){
        console.log(document.getElementById("reContent"+parentId).value);
        let reContent=document.getElementById("reContent"+parentId).value;
        console.log(reContent);
        if(reContent.trim()==''){alert("评论不能为空!");return;}
        let xhr=new XMLHttpRequest();
        xhr.open("post","http://localhost:80/sensitiveCheck",true);
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        xhr.responseType='json';
        xhr.send("content="+JSON.stringify(reContent));
        xhr.onreadystatechange=function(){;
            if(xhr.status==200 && xhr.readyState==4){
                let data = xhr.response;
                console.log(data);
                if(data["code"]==200){
                    console.log("check200:"+data.state)
                    insertMessage(content,senderId,reContent,parentId)
                }else{
                    alert(data.state);
                    console.log("check404:"+data.state)
                }
            }
        }
    }
    function insertMessage(content,senderId,reContent,parentId){
        //1.给这条评论的母体主人发送通知notice--content state recipientId
        //2.给这家店铺新增message-- shopId reContent userId
        let json={
            "content":getCookie("name")+"评论了您的评论["+
                content+"]->"+reContent,
            "state":false,
            "recipientId":senderId,
            "shopId":id,
            "reContent":reContent,
            "parentId":parentId,
            "userId":getCookie("id"),//发起这条评论的人
            "mode":"reContent"
        }
        let xhr=new XMLHttpRequest();
        xhr.open("post","http://localhost:80/notice",true);
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        console.log(JSON.stringify(json))
        xhr.send("json="+JSON.stringify(json));
        xhr.onreadystatechange=function (){
            if(xhr.status==200 && xhr.readyState==4){
                let data=xhr.response;
                let json=JSON.parse(data);
                console.log(json);
                alert(json.state);
                window.location="Message.jsp?shopId="+id;
            }
        }
    }
    function like(content,senderId,parentId){
        let notice={
            "content":getCookie("name")+"点赞了您的评论["+
                content+"]",
            "state":false,
            "recipientId":senderId,
            "parentId":parentId,
            "mode":"likes"
        }
        let xhr=new XMLHttpRequest();
        xhr.open("post","http://localhost:80/notice",true);
        xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        console.log(JSON.stringify(notice))
        xhr.send("json="+JSON.stringify(notice));
        xhr.onreadystatechange=function (){
            if(xhr.status==200 && xhr.readyState==4){
                let data=xhr.response;
                let json=JSON.parse(data);
                console.log(json);
                alert(json.state);
                window.location="Message.jsp?shopId="+id;
            }
        }
    }
    getMessage();
    window.back=function (){
        window.location="IntoShop.jsp?shopId="+id;
    }

</script>
</html>
